package com.globallogic.webapp.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.globallogic.webapp.javaclasses.Users;

@WebServlet(description = "Users registration", urlPatterns = { "/Registration" })
public class Registration extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public Registration() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
			response.setContentType("text/html;charset=UTF-8");
			StringBuilder sb = new StringBuilder();
			String login = request.getParameter("login");
			String inputPassword = request.getParameter("password");
			String confirmPassword = request.getParameter("confirmPassword");
			Users users = new Users();
			HttpSession session = request.getSession();
			if (!users.getUsers().containsKey(login)) {
				if (inputPassword.equals(confirmPassword)) {
					users.addUser(login, inputPassword);
					sb.append("<br>Thanks for registration. You can sign-in with you login");
					sb.append("<br> <a href=\'index.jsp\'>Login Page</a>");
					session.setAttribute("str", sb.toString());
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("Message.jsp");
					dispatcher.forward(request, response);
				}else{
					sb.append("Passwords is not the same!");
					sb.append("<br> <a href=\'Registration.jsp\'> Registation Page</a>");
					session.setAttribute("str", sb.toString());
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("Message.jsp");
					dispatcher.forward(request, response);
				}
			} else {
				sb.append("This login is already exist. Choose another!");
				sb.append("<br> <a href=\'Registration.jsp\'>Registation Page</a>");
				session.setAttribute("str", sb.toString());
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("Message.jsp");
				dispatcher.forward(request, response);
			}
	}
	

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
	}

	

}
